﻿using DevLib.Logger;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


/** 
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
 * Description: Use multi-cast delegate for check string arg and string[] args.
 * Author: Heller (HellerSong@outlook.com)
 * CreationTime: 3/17/2015 8:28:31 AM
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** 
 **/
namespace DevLib.TestCase
{
	public class StrCheck
	{
		// The delegate of check for string type argument
		public static Action<String> CheckChainArgNotNull = null;
		public static Action<String> CheckChainArgUsable = null;
		public static Action<String> CheckChainDirIllegal = null;
		public static Action<String> CheckChainDirOperable = null;
		public static Action<String> CheckChainFileOperable = null;
		// The delegate of check for string array type argument
		public static Action<String[]> CheckChainArgNotNullArray = null;
		public static Action<String[]> CheckChainArgUsableArray = null;

		// Constrctor with static should not public, protected as call by .Net system
		static StrCheck()
		{
			/*
			 * String Type Checking
			 */
			CheckChainArgNotNull += DoesArgumentNull;

			// Check not null, not empty
			CheckChainArgUsable += DoesArgumentNull;
			CheckChainArgUsable += DoesArgumentEmpty;

			// Check not null, not empty, no invalid chars
			CheckChainDirIllegal += CheckChainArgUsable;
			CheckChainDirIllegal += DoesPathContainInvalidChars;

			// Additional check path exists
			CheckChainDirOperable += CheckChainDirIllegal;
			CheckChainDirOperable += DoesPathExists;

			// Additional check file exists
			CheckChainFileOperable += CheckChainDirIllegal;
			CheckChainFileOperable += DoesFileExists;


			/*
			 * String Array Type Checking
			 */
			CheckChainArgNotNullArray += DoesArgumentArrayNull;

			// Check not null, not empty
			CheckChainArgUsableArray += DoesArgumentArrayEmpty;
		}


		#region Test Case cover some basic situation: not null and not empty with spical data type
		public static void DoesArgumentNull(String arg)
		{
			if (arg == null)
			{
				String msg = "Argument should not null";
				DevLogger.WriteLog(msg);
				throw new ArgumentException(DevLogger.GetCallingExceptionMessage(msg));
			}
		}
		public static void DoesArgumentEmpty(String arg)
		{
			if (arg.Length == 0)
			{
				String msg = "Argument should not empty";
				DevLogger.WriteLog(msg);
				throw new ArgumentException(DevLogger.GetCallingExceptionMessage(msg));
			}
		}
		public static void DoesArgumentArrayNull(String[] arg)
		{
			if (arg == null)
			{
				String msg = "Argument Array should not null";
				DevLogger.WriteLog(msg);
				throw new ArgumentException(DevLogger.GetCallingExceptionMessage(msg));
			}
		}
		public static void DoesArgumentArrayEmpty(String[] arg)
		{
			if (arg.Length == 0)
			{
				String msg = "Argument Array should not empty";
				DevLogger.WriteLog(msg);
				throw new ArgumentException(DevLogger.GetCallingExceptionMessage(msg));
			}
		}
		#endregion


		#region Test Case cover custom situation
		public static void DoesPathExists(String path)
		{
			if (!Directory.Exists(path))
			{
				String msg = "Target direcroty not found";
				DevLogger.WriteLog(msg);
				throw new Exception(DevLogger.GetCallingExceptionMessage(msg));
			}
		}
		public static void DoesFileExists(String filePath)
		{
			if (!File.Exists(filePath))
			{
				String msg = "Target file not found";
				DevLogger.WriteLog(msg);
				throw new Exception(DevLogger.GetCallingExceptionMessage(msg));
			}
		}
		public static void DoesPathContainInvalidChars(String path)
		{
			for (int i = 0; i < path.Length; i++)
			{
				// Normally in windows file system, should not contcain \ / : * ? " < > | 
				int num = path[i];
				if (((num == 0x22) || (num == 60)) || (((num == 0x3e) || (num == 0x7c)) || (num < 0x20)))
				{
					// Should not contain \  "  <  >  |
					String msg = "Argument has invalid chars";
					DevLogger.WriteLog(msg);
					throw new ArgumentException(DevLogger.GetCallingExceptionMessage(msg));
				}
			}
		}
		#endregion


	}
}
